package edu.siu.math.egut.util;

import java.io.Serializable;
import java.util.ArrayList;

import edu.siu.math.egut.main.OutputStyle;

/**
 * @author josephhundley
 *
 */
public class Parabolic implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1497701551749733986L;
    private final Group g;
    private final int corank;
    private final int[] specifier;

    public Parabolic(Group g, int corank, int[] specifier) {
	super();
	this.g = g;
	this.corank = corank;
	this.specifier = specifier;
    }
    public int getCorank(){
	return corank;
    }
    public static Parabolic create(Group group, int i) {
	int[] specifier = new int[1];
	specifier[0]=i;
	return new Parabolic(group, 1, specifier);
    }
    public Piece getPiece(int i){
	int[] spec = new int[1];
	spec[0]=i;
	return Piece.create(this,spec);
    }
    public static Parabolic read(Group g, String string) throws ParabolicReadException {
	int[] specifier = edu.siu.math.egut.io.Algorithms.tupleFromString(string);
	if(isAParabolicSpecifier(g,specifier))
	    return new Parabolic(g, specifier.length, specifier);
	throw new ParabolicReadException();
    }
    private static boolean isAParabolicSpecifier(Group g2, int[] specifier2) {
	System.out.println("group = "+g2);
	System.out.println("specifier ="+edu.siu.math.egut.io.Algorithms.tupleToString(specifier2));
	if(specifier2.length > g2.getRank()){
	    System.out.println("not a specifier because" + specifier2.length +" > " +g2.getRank());
	    return false;
	}
	for( int i = 0 ; i < specifier2.length; i++){
	    if( (specifier2[i]<=0) || (specifier2[i] > g2.getRank()) ){
		   System.out.println("not a specifier because" + specifier2[i] +"is out of range");

		return false;
	    }
	    for( int j = i+1; j < specifier2.length; j++)
		if(specifier2[j]<= specifier2[i]){
		    System.out.println("not a specifier because" + specifier2[j] +" <= " +specifier2[i]);
		    
		    return false;
		}
	    
	}
	System.out.println("is a specifier.");
	return true;
	    
    }
    /**
     * The roots of our fixed maximal torus in the parabolic in question.
     * @return the set of roots
     */
    public ArrayList<int[]> getRoots() {
	final ArrayList<int[]> roots =  new ArrayList<int[]>() ;
	for(final int[] r: g.getRoots()){
	    if(contains(r))
		roots.add(r);
	    
	}
	return roots;
	    
		
	
    }
    private boolean contains(int[] r) {
	for( int j: specifier)
	    if(r[j-1] < 0)
		return false;
	return true;
    }
    public String getSpecifier() {
	
	return getSpecifier(OutputStyle.LIE);
    }

    public String getSpecifier(OutputStyle sty) {
	switch (sty) {
	case LIE:
	    return edu.siu.math.egut.io.Algorithms.tupleToString(specifier);

	default:
	    return this.getSpecifier();
	}
    }

}
